/**
 * Copyright (c) 2010 sonntagsfarben.de
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package de.sonntagsfarben.puremvc.as3.proxy
{
	import com.asual.swfaddress.SWFAddress;
	import com.asual.swfaddress.SWFAddressEvent;
	
	import de.sonntagsfarben.utils.SWFAddressUtil;
	
	import flash.external.ExternalInterface;
	import flash.utils.getTimer;
	
	import org.puremvc.as3.multicore.patterns.proxy.Proxy;

	/**
	 * @author Soenke Kluth <kluth@sonntagsfarben.de>
	 */
	public class SWFAddressProxy extends Proxy
	{
		public static const NAME:String = "SWFAddressProxy";

		public static const ADDRESS_CHANGE:String = SWFAddressProxy.NAME + "_SECTION_CHANGE";
		public static const TITLE_CHANGE:String = SWFAddressProxy.NAME + "_TITLE_CHANGE";

		private var _lastAddress:String;
		private var _addressSegments:Array;

		private var _escapeCheckTimer:uint;
		private var _siteName:String = "";


		public function SWFAddressProxy()
		{
			super(NAME, new String);
			_addressSegments = new Array();
			_initSWFAdress();
		}



		//**** private *****//
		private function _initSWFAdress():void
		{
			SWFAddress.addEventListener(SWFAddressEvent.CHANGE, onAddressChange);
		}


		private function _setAddress(addressSegments:Array):void
		{
			
			var timer:uint = getTimer();

			if (timer - _escapeCheckTimer < 200 && address.match(" "))
			{
				trace(this + ":: preventing blank character bug! dont tell the app the address changed!");
				return;
			}

			_escapeCheckTimer = getTimer();

			_addressSegments = addressSegments;

			sendNotification(ADDRESS_CHANGE, _addressSegments, "Array");
			sendNotification(TITLE_CHANGE, siteName + SWFAddressUtil.formatTitleSeg(_addressSegments));
		}



		//**** event handlers ****//

		private function onAddressChange(event:SWFAddressEvent):void
		{
			_lastAddress = address;
			data = event.value;
			_setAddress(SWFAddressUtil.segmentURI(event.value));
		}



		//**** public ****//

		public function setTitle(title:String):void
		{
			SWFAddress.setTitle(title);
		}


		public function requestAddress(uri:String):void
		{
			if (ExternalInterface.available)
			{
				SWFAddress.setValue(uri);
			}
			else
			{
				var segments:Array = uri.split("/");
				if(String(segments[segments.length-1]).length < 1){
					segments.pop();
				}
				
				_setAddress(segments);
			}
		}



		public function set siteName(siteName:String):void
		{
			_siteName = siteName;
		}


		public function get siteName():String
		{
			return _siteName;
		}


		public function get lastAddressSegments():Array
		{
			return SWFAddressUtil.segmentURI(lastAdress);
		}


		public function get lastAdress():String
		{
			return _lastAddress;
		}


		public function get address():String
		{
			return data as String;
		}


		public function get addressSegments():Array
		{
			return _addressSegments;
		}


		//***** override *****//

		override public function onRegister():void
		{
		}


		override public function onRemove():void
		{
		}
	}
}
